package com.hfh.main.file.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfh.main.common.annotate.JwtToken;
import com.hfh.main.common.resp.Result;
import com.hfh.main.file.entity.FileEntity;
import com.hfh.main.file.mapper.FileMapper;
import com.hfh.main.file.service.FileService;
import com.hfh.main.group.entity.GroupDetailEntity;
import com.hfh.main.group.service.GroupDetailService;
import com.hfh.main.user.entity.UserEntity;
import com.hfh.main.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 日报 前端控制器
 * </p>
 */
@Tag(name = "分值权重、文件管理")
@RestController
@RequestMapping("/fileEntity")
public class FileController {

    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private FileService fileService;
    @Autowired
    private UserService userService;
    @Autowired
    private GroupDetailService goupDetailService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @JwtToken
    @Operation(summary = "列表-分页")
    @PostMapping("/listPage")
    public Result<?> listPage(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) String acc) {
        LambdaQueryWrapper<GroupDetailEntity> detail1 = new LambdaQueryWrapper<>();
        detail1.eq(GroupDetailEntity::getAcc, acc);
        GroupDetailEntity one = goupDetailService.getOne(detail1);
        LambdaQueryWrapper<GroupDetailEntity> detail2 = new LambdaQueryWrapper<>();
        detail2.eq(GroupDetailEntity::getGroupId, one.getGroupId());
        List<GroupDetailEntity> listAcc = goupDetailService.list(detail2);
        List<String> listAccccccc = listAcc.stream().map(GroupDetailEntity::getAcc).toList();

        if(page == null || page < 1){
            page = 1;
        }

        if(pageSize == null || pageSize < 20){
            page = 20;
        }

        //分页参数
        Page<FileEntity> rowPage = new Page(page, pageSize);
        //queryWrapper组装查询where条件
        LambdaQueryWrapper<FileEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(FileEntity::getUserAcc, listAccccccc);
        queryWrapper.orderByDesc(FileEntity::getCreateTime);
        rowPage = fileMapper.selectPage(rowPage, queryWrapper);
        List<FileEntity> records = rowPage.getRecords();
        List<FileEntity> list = records.stream().map(m -> {
            UserEntity userEntity = userService.queryByAcc(m.getUserAcc());
            m.setUserName(userEntity.getName());
            return m;
        }).toList();
        rowPage.setRecords(list);
        return Result.success("数据请求成功", rowPage);
    }

    @JwtToken
    @Operation(summary = "保存")
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody @Validated FileEntity entity) {
        QueryWrapper<FileEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("id"), entity.getId());
        if (fileService.count(wrapper) != 0) return Result.failure(500, "编号已存在", sdf.format(new Date()));
        entity.setCreateTime(new Date());
        String url = entity.getUrl();
        String filename = url.substring(url.lastIndexOf('/') + 1);
        entity.setFileName(filename);
        fileService.save(entity);
        return Result.success("保存成功", sdf.format(new Date()));
    }

    @JwtToken
    @Operation(summary = "删除")
    @PostMapping("/delete/{param}")
    public Result<?> delete(@PathVariable @Validated String param) {
        QueryWrapper<FileEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(("id"), param);
        if (fileService.count(wrapper) == 0) return Result.failure(500, "编号不存在", sdf.format(new Date()));
        fileService.remove(wrapper);
        return Result.success("保存成功", sdf.format(new Date()));
    }

}
